package com.woniu.dao;

import com.woniu.entity.Category;
import com.woniu.entity.CategoryCourse;
import com.woniu.entity.Course;
import com.woniu.entity.User;
import com.woniu.entity.vo.CourseVO;
import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.Pageable;
import java.util.List;

/**
 * (Course)表数据库访问层
 *
 * @author makejava
 * @since 2025-06-09 18:58:14
 */
public interface CourseDao {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    Course queryById(Long id);

    // 批量查询课程
    List<Course> selectByIds(@Param("ids") List<Long> courseIds);

    List<Course> queryByIds( List<Long> courseIds);
    /**
     * 查询指定行数据
     *
     * @param course 查询条件
     * @param pageable         分页对象
     * @return 对象列表
     */
    List<Course> queryAllByLimit(Course course, @Param("pageable") Pageable pageable);

    /**
     * 统计总行数
     *
     * @param course 查询条件
     * @return 总行数
     */
    long count(Course course);

    /**
     * 新增数据
     *
     * @param course 实例对象
     * @return 影响行数
     */
    int insert(Course course);

    /**
     * 批量新增数据（MyBatis原生foreach方法）
     *
     * @param entities List<Course> 实例对象列表
     * @return 影响行数
     */
    int insertBatch(@Param("entities") List<Course> entities);

    /**
     * 批量新增或按主键更新数据（MyBatis原生foreach方法）
     *
     * @param entities List<Course> 实例对象列表
     * @return 影响行数
     * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常，请自行校验入参
     */
    int insertOrUpdateBatch(@Param("entities") List<Course> entities);

    /**
     * 修改数据
     *
     * @param course 实例对象
     * @return 影响行数
     */
    int update(Course course);

    @Select("select * from course where id = #{id}")
    Course getCourseById(Long id);

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    int deleteById(Long id);

    @Select({
            "<script>",
            "SELECT c.*, u.*,cuc.collect_id as collectId",
            "FROM course c",
            "LEFT JOIN user u ON c.teacher_id = u.user_id",
            "left join collect_user_course cuc on c.id=cuc.cid and cuc.uid = #{uid}",
            "WHERE c.id = #{courseId}",
            "</script>"
    })
    CourseVO getCourseVOById(@Param("courseId") Integer courseId,@Param("uid") Integer uid);

    @Select("select * from course")
    List<Course> findAllCourse();

    /**
     * 根据分类id查询课程id
     * @param categoryId
     * @return
     */
    @Select("select course_id from category_course where category_ids = #{categoryId}")
    List<Long> queryCourseIdByCategoryId(Integer categoryId);

    /**
     * 查询指定父分类下的直接子分类
     */
    @Select("select * from category where pid = #{id} and is_delete = 0 ")
    List<Category> findDirectSubCategories(Integer id);

    List<Course> findByCondition(
            @Param("title") String title,
//            @Param("teacherId") Integer teacherId,
            @Param("teacherName") String teacherName,
            @Param("enable") Integer enable);
    /**
     * 推荐课程（观看人数前四）
     */
    @Select("select * from course where enable=0 order by learn_count desc limit 4")
    List<Course> getRecommendCourses();

    /**
     * 最受欢迎的课程(学习人数最多的人)
     */
    @Select("select * from course where enable=0 order by view_count desc limit 4")
    List<Course> getPopularCourses();

    /**
     * 添加课程类别关联
     */
    @Insert("INSERT INTO category_course (category_ids, course_id) VALUES (#{categoryId}, #{courseId})")
    void addCourseCategory(CategoryCourse categoryCourse);

    /**
     * 获取课程关联的类别
     */
    @Select("SELECT category_ids as categoryId, course_id as courseId FROM category_course WHERE course_id = #{courseId}")
    List<CategoryCourse> getCourseCategories(Integer courseId);

    /**
     * 删除课程关联的类别
     */
    @Delete("DELETE FROM category_course WHERE course_id = #{courseId}")
    void deleteCourseCategories(Long courseId);

    // 检查是否已存在相同关联
    @Select("SELECT * FROM category_course WHERE course_id = #{courseId} AND category_ids = #{categoryId} LIMIT 1")
    CategoryCourse checkCategoryCourseExist(@Param("courseId") Long courseId, @Param("categoryId") Integer categoryId);

    //收藏榜
    //根据需求是否加入enable = 0条件，我这里暂时不加入
    @Select("SELECT * FROM course ORDER BY CAST(favorite AS SIGNED) DESC LIMIT 5")
    List<Course> getFavoriteRankings();

    @Select("SELECT COUNT(*) FROM course WHERE teacher_id = #{teacherId}")
    int countByTeacherId(Long teacherId);

    @Select("SELECT * FROM course WHERE teacher_id = #{teacherId}")
    List<Course> getCourseByTeacherId(Long teacherId);
}

